<?php
namespace App\Service\Member;

use Illuminate\Support\Facades\DB;

class PeizhenService extends BaseService
{
    //陪诊

    /**
     * 列表
     * @param array $pages【start，limit】
     */
    public function getList($pages)
    {
        $start = ($pages['start'] - 1) * $pages['limit'];
        $db = $this->dbConn($this->dbPeizhen)
            ->where('del', 0);
        $total = $db->count();
        $res = $db->skip($start)->take($pages['limit'])
            ->orderBy('id', 'desc')
            ->get();
        if (!count($res)) {
            //return $this->toback(-2, '没有记录');
            return $this->toback();
        }
        $arys = [];
        foreach ($res as $v) {
            $v = (array) $v;
            //发布人
            $uOne = (array) $this->dbConn($this->dbUser)
                ->where('id', $v['pat_uid'])
                ->first();
            $pat_head = ''; //病人头像
            $pat_sex = ''; //病人性别
            $pat_age = ''; //病人年龄
            if (!empty($uOne)) {
                $pat_head = file_exists($uOne['head']) ? env('DOMAIN').'/'.$uOne['head'] : '';
                $pat_sex = $uOne['sex']==1 ? '男' : '女';
                $pat_age = $uOne['age'].'岁';
            }
            //医院
            $yiyuanOne = (array) $this->dbConn($this->dbYiyuan)
                ->where('id', $v['yiyuan'])
                ->first();
            $yiyuan_name = empty($yiyuanOne) ? '-' : $yiyuanOne['title'];
            $arys[] = [
                'id' => $v['id'],
                'title' => $v['title'],
                'pat_name' => $v['pat_name'],
                'pat_head' => $pat_head,
                'pat_sex' => $pat_sex,
                'pat_age' => $pat_age,
                'yiyuan_name' => $yiyuan_name,
                'start_time' => $v['start_time'],
                'price' => toNumber($v['price']),
                'distance' => '3.3km',
            ];
        }
        return $this->toback(0, '', $arys, $total);
    }

    /**
     * 详情
     */
    public function getOne($id)
    {
        $res = (array) $this->dbConn($this->dbPeizhen)
            ->where('id', $id)
            ->first();
        if (empty($res)) {
            //return $this->toback(-2, '没有记录');
            return $this->toback();
        }
        //医院
        $yiyuanOne = (array) $this->dbConn($this->dbYiyuan)
            ->where('id', $res['yiyuan'])
            ->first();
      //  $yiyuan_name = empty($yiyuanOne) ? '-' : $yiyuanOne['title'];
        $arys = [
            'id' => $res['id'],
            'title' => $res['title'],
            'yiyuan' => $res['yiyuan'],
            'pat_phone' => $res['pat_phone'],
           // 'yiyuan_name' => $yiyuan_name,
            'delay'=>$res['delay'],
            'remark' => $res['remark'],
        ];
        if($yiyuanOne){
            $arys['yiyuan_name']=$yiyuanOne['title'];
            $arys['yiyuan_address']=$yiyuanOne['address'];
            $arys['yiyuan_map']=$yiyuanOne['map'];
        }
        return $this->toback(0, '', $arys);
    }

    /**
     * 发布
     * @param array $pas
     */
    public function add($pas)
    {
        $yiyuanOne = (array) $this->dbConn($this->dbYiyuan)
            ->where('del', 0)
            ->where('title', $pas['yiyuan'])
            ->first();
        if (empty($yiyuanOne)) {
            return $this->toback(-2, '医院错误');
        }
        $pas['yiyuan'] = $yiyuanOne['id'];
        $pas['addtime'] = toDate();
        DB::beginTransaction(); //开启事务
        try {
            $lastid = $this->dbConn($this->dbPeizhen)->insertGetId($pas);
            if (!$lastid) {
                return $this->toback(-1, 'error');
            }
            //广场处理
            $this->dbConn($this->dbGc)->insertGetId([
                'type' => 3, 'oid' => $lastid, 'addtime' => toDate(),
            ]);
            DB::commit(); //提交事务
            $ary = [
                'yw_type' => 5,
                'yw_id' => $lastid,
            ];
            return $this->toback(0, '', $ary);
        } catch (\Exception $e) {
            DB::rollBack(); //事务回滚
            echo $e->getMessage();
            return $this->toback(-1, 'error');
        }
    }

    /**
     * 医生接单
     * @param int $id
     * @param int $uid
     */
    public function rob($id, $uid)
    {
        $res = (array) $this->dbConn($this->dbPeizhen)
            ->where('del', 0)
            ->where('id', $id)
            ->where('dot_uid', 0)
            ->first();
        if (empty($res)) {
            return $this->toback(-2, '没有记录');
            //return $this->toback();
        }
        if ($res['status'] > 2) {
            return $this->toback(-2, '进行中');
        }
        $up = $this->dbConn($this->dbPeizhen)
            ->where('id', $id)
            ->update(['status' => 3, 'dot_uid' => $uid, 'robtime' => toDate()]);
        if (!$up) {
            return $this->toback(-1, 'error');
        }
        $patOne = (array) $this->dbConn($this->dbUser)
            ->where('id', $res['pat_uid'])
            ->first();
        $arys = [
            'pat_phone' => !empty($patOne)&&$patOne['phone'] ? $patOne['phone'] : '',
        ];
        return $this->toback(0, '', $arys);
    }

    /**
     * 病情评估
     * @param int $id
     */
    public function close($id, $remark)
    {
        $res = (array) $this->dbConn($this->dbPeizhen)
            ->where('del', 0)
            ->where('id', $id)
            ->first();
        if (empty($res)) {
            //return $this->toback(-2, '没有记录');
            return $this->toback();
        }
        if ($res['is_close']) {
            return $this->toback(-2, '已取消');
        }
        if ($res['status'] > 3) {
            return $this->toback(-2, '进行中');
        }
        $up = $this->dbConn($this->dbPeizhen)
            ->where('id', $id)
            ->update(['is_close' => 1, 'close_remark' => $remark, 'closetime' => toDate()]);
        if (!$up) {
            return $this->toback(-1, 'error');
        }
        return $this->toback();
    }

    /**
     * 服务开始
     * @param int $id
     */
    public function keep($id)
    {
        $res = (array) $this->dbConn($this->dbPeizhen)
            ->where('del', 0)
            ->where('id', $id)
            ->first();
        if (empty($res)) {
            //return $this->toback(-2, '没有记录');
            return $this->toback();
        }
        if ($res['status'] > 4) {
            return $this->toback(-2, '进行中');
        }
        $up = $this->dbConn($this->dbPeizhen)
            ->where('id', $id)
            ->update(['status' => 4, 'keeptime' => toDate()]);
        if (!$up) {
            return $this->toback(-1, 'error');
        }
        return $this->toback();
    }

    /**
     * 服务确认
     * @param int $id
     */
    public function finish($id)
    {
        $res = (array) $this->dbConn($this->dbPeizhen)
            ->where('del', 0)
            ->where('id', $id)
            ->first();
        if (empty($res)) {
            //return $this->toback(-2, '没有记录');
            return $this->toback();
        }
        if ($res['is_close']) {
            return $this->toback(-2, '已取消');
        }
        if ($res['status'] == 5) {
            return $this->toback(-2, '已完成');
        }
        $up = $this->dbConn($this->dbPeizhen)
            ->where('id', $id)
            ->update(['status' => 5, 'finishtime' => toDate()]);
        if (!$up) {
            return $this->toback(-1, 'error');
        }
        return $this->toback();
    }

    /**
     * 陪诊价格
     * 按时长计算
     * @param int $delay N个小时
     */
    public function price($delay)
    {
        $arys = [
            'price' => 0,
        ];
        $res = (array) $this->dbConn($this->dbConfig)
            ->where('id', 1)
            ->first();
        if (empty($res)) {
            return $this->toback(0, '', $arys);
        }
        $arys['price'] = toNumber($res['val'] * $delay);
        return $this->toback(0, '', $arys);
    }
}
